import re
import ast
import os


class AbstractCache:
    def set(self, key, val):
        pass

    def get(self, key):
        pass


class FileCache(AbstractCache):

    def getPath(self, key):
        path = os.path.dirname(__file__) + '\\..\\run\\cache\\' + key
        # path = os.path.dirname(__file__) + key
        return path

    def set(self, key, val):
        val = str(val)
        path = self.getPath(key)
        f = open(path, 'w')
        val = str(val)
        f.write(val)
        f.close()

    def get(self, key):
        path = self.getPath(key)
        if not os.path.exists(path):
            return None
        f = open(path)
        res = f.read()
        if not res:
            return None
        try:
            return ast.literal_eval(res)
        except BaseException:
            return res


engine = 'file'  # 临时配置
cache = FileCache()
# cache = AbstractCache()
# if engine == 'file':
# 	cache = FileCache()
